﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Roboter
{
    class ReadFile
    {
        private Robot robot;

        internal Robot Robot
        {
            get { return robot; }
            set { robot = value; }
        }
        private Board board;

        internal Board Board
        {
            get { return board; }
            set { board = value; }
        }
        private List<Obstacle> obstacles = new List<Obstacle>();

        internal List<Obstacle> Obstacles
        {
            get { return obstacles; }
            set { obstacles = value; }
        }
        private List<string> input = new List<string>();



        public ReadFile(string source)
        {            
            try
            {

                string readIn;
                StreamReader reader = new StreamReader(source);

                while ((readIn = reader.ReadLine()) != null)
                {
                    input.Add(readIn);
                }

                


                //extract board information
                string[] temp = input[0].Split(' ');
                int width;
                int height;

                Int32.TryParse(temp[0], out width);
                Int32.TryParse(temp[1], out height);

                this.board = new Board(width, height);

                //extract robot information
                temp = input[1].Split(' ');
                int tempX;
                int tempY;
                Int32.TryParse(temp[0], out tempX);
                Int32.TryParse(temp[1], out tempY);

                this.robot = new Robot(tempX, tempY);

                //extract exit information
                temp = input[2].Split(' ');
                int tempX1;
                int tempY1;
                int tempX2;
                int tempY2;
                Int32.TryParse(temp[0], out tempX1);
                Int32.TryParse(temp[1], out tempY1);
                Int32.TryParse(temp[2], out tempX2);
                Int32.TryParse(temp[3], out tempY2);

                this.obstacles.Add(new Obstacle(tempX1, tempY1, tempX2, tempY2, true));

                //extract obstacle information
                
                int numberOfObstacles;
                Int32.TryParse(input[3], out numberOfObstacles);

                for (int i = 0; i < numberOfObstacles; i++)
                {
                    temp = input[4 + i].Split(' ');
                    Int32.TryParse(temp[0], out tempX1);
                    Int32.TryParse(temp[1], out tempY1);
                    Int32.TryParse(temp[2], out tempX2);
                    Int32.TryParse(temp[3], out tempY2);

                    this.obstacles.Add(new Obstacle(tempX1, tempY1, tempX2, tempY2));
                }
            }
            catch (Exception e)
            {
                global::System.Windows.MessageBox.Show("File could not be read : \n" + e.ToString() ); ;
            }

        }
    }
}
